function confimDestroyCart () {
        $("#dialogDestroyCart").dialog("open");
}

$(function() {
    var pathImgLoader = "<img src='"+ window.location.origin +"/DienThoai213/public/frontend/images/ajax-loader.gif'>";
    var nameRegister = $( "#nameRegister" ),
        emailRegister = $( "#emailRegister" ), // Email form Dang ky
        phoneRegister = $( "#phoneRegister" ),
        passwordRegister = $( "#passwordRegister" ),
        rePasswordRegister = $( "#rePasswordRegister" ),
        companyRegister = $('#companyRegister'),
        areaRegister = $('#areaRegister'),
        allFields = $( [] ).add(nameRegister).add(emailRegister).add(passwordRegister).add(phoneRegister).add(companyRegister).add(areaRegister).add(rePasswordRegister);

    function updateTips( t ) {
        var  tips = $( ".validateTips" );
        tips
            .text( t )
            .addClass( "ui-state-highlight" );
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 1500 );
        }, 500 );
    }

    function checkLength( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( "Độ dài " + n + " phải lớn hơn " +
                min + " và nhỏ hơn " + max + "." );
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else {
                            
            return true;
        }
    }

    function checkPassword(repass,pass,n){
        debugger
        if(pass.val() != repass.val()){
            repass.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else{
            return true;    
        }   
    }

    function checkLength2( msg, n ) {
        if ( msg.val().length <= 0 ) {
            msg.addClass( "ui-state-error" );
            updateTips( n+ " không được để trống.");
            return false;
        } else {
            return true;
        }
    }

    $( "#dialog-modal" ).dialog({
        autoOpen: false,             
        width: 700,
        height: 500,
        modal: true,
        open: function(event, ui)
        {
            
        },
        
        buttons: {
            "Dùng Thử": function() { console.log(areaRegister.val());
                allFields.removeClass( "ui-state-error" );
                var bValid = true;
                bValid = bValid && checkLength2(nameRegister, 'Name');
                bValid = bValid && checkLength( nameRegister, "name", 6, 50 );
                
                bValid = bValid && checkRegexp( emailRegister, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Email sai định dạng. Email đúng có dạng: yourname@gmail.com" );
                bValid = bValid && checkLength( phoneRegister, "phone", 1, 50 );
                bValid = bValid && checkLength( passwordRegister, "password", 1, 50 );
                bValid = bValid && checkPassword( rePasswordRegister, passwordRegister,"Password nhập lại không đúng." );
            },

            'Đóng': function() {
                $(this).dialog("close");

            }
        }
    });

    $( "#dialogDestroyCart" ).dialog({
        autoOpen: false,             
        width: 400,
        height: 150,
        modal: true,
        resizable: false,
        open: function(event, ui)
        {
            
        },
        buttons: {
            'Xóa': function() {
                $("#notificationsLoader").html(pathImgLoader);
                $.ajax({  
                    type: "POST",  
                    url: window.location.origin + "/DienThoai213/cart/destroyCart",  
                    success: function(theResponse) {
                        setTimeout(
                        function()
                        {   
                            $("#basketItemsWrap").html(theResponse);
                            $("#notificationsLoader").empty();

                        }, 500);
                    }  

                });
                $(this).dialog('close');
            },
            'Đóng': function() {
                $(this).dialog("close");

            }
        }
    });
});